package com.hudongwx.sadpw.fragment.community;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hudongwx.sadpw.MyJsonObject;
import com.hudongwx.sadpw.R;
import com.hudongwx.sadpw.activity.TieZiDetailActivity;

import org.json.JSONArray;
import org.json.JSONException;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

/**
 * A simple {@link Fragment} subclass.
 */
@ContentView(R.layout.fragment_ydj)
public class YDJSFragment extends Fragment {

    private View view;
    String TAG="WLSDFragment";
    JSONArray jsonArray;
    private MyWlsdAdapter myWlsdAdapter;
    @ViewInject(R.id.wlsd_rec)
    private RecyclerView recyclerView;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (null==view){
            view= x.view().inject(this,inflater,container);
            GridLayoutManager gridLayoutManager =new GridLayoutManager(getActivity(),1);
            recyclerView.setLayoutManager(gridLayoutManager);
            myWlsdAdapter=new MyWlsdAdapter(jsonArray);
            recyclerView.setAdapter(myWlsdAdapter);
            parseWlsd();
        }
        return view;
    }
    class MyWlsdAdapter extends RecyclerView.Adapter<MyWlsdViewHolder>{
        public MyWlsdAdapter(JSONArray jsonArray) {
            this.jsonArray = jsonArray;
        }
        JSONArray jsonArray;
        public void updateItem(JSONArray jsonArray){
            this.jsonArray=jsonArray;
            notifyDataSetChanged();
        }
        @Override
        public MyWlsdViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(getContext()).inflate(R.layout.community_4, null);
            return new MyWlsdViewHolder(view);
        }

        @Override
        public void onBindViewHolder(MyWlsdViewHolder holder, final int position) {
            if (null==jsonArray){
                recyclerView.setVisibility(View.GONE);
            }else {
                try {
                    x.image().bind(holder.imageView1,jsonArray.getJSONObject(position).getString("UserIcon"));
                    holder.textView1.setText(jsonArray.getJSONObject(position).getString("UserName"));
                    holder.textView2.setText(jsonArray.getJSONObject(position).getString("DateTime"));
                    if (jsonArray.getJSONObject(position).getString("CommentCount").equals("0")){
                        holder.textView3.setText("回帖");
                    }else{
                        holder.textView3.setText(jsonArray.getJSONObject(position).getString("CommentCount"));
                    }
                    holder.textView4.setText(jsonArray.getJSONObject(position).getString("Title"));
                    if (jsonArray.getJSONObject(position).getString("Content").equals("")){
                        holder.textView5.setVisibility(View.GONE);
                        holder.textView5.setText("");
                    }else{
                        holder.textView5.setText(jsonArray.getJSONObject(position).getString("Content"));
                    }
                    JSONArray array = jsonArray.getJSONObject(position).getJSONArray("ListFile");
                        if (array.length()==1){
                            x.image().bind(holder.imageView3,array.getJSONObject(0).getString("Url"));
                        }else if (array.length()==2){
                            x.image().bind(holder.imageView3,array.getJSONObject(0).getString("Url"));
                            x.image().bind(holder.imageView4,array.getJSONObject(1).getString("Url"));
                        }else if (array.length()>=3){
                            x.image().bind(holder.imageView3,array.getJSONObject(0).getString("Url"));
                            x.image().bind(holder.imageView4,array.getJSONObject(1).getString("Url"));
                            x.image().bind(holder.imageView5,array.getJSONObject(2).getString("Url"));
                        }else if (array.length()==0){
                            holder.ll.setVisibility(View.GONE);
                            holder.imageView3.setVisibility(View.GONE);
                            holder.imageView4.setVisibility(View.GONE);
                            holder.imageView5.setVisibility(View.GONE);
                        }
                } catch (JSONException e) {
                    e.printStackTrace();
                }



                holder.ll1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent=new Intent(v.getContext(), TieZiDetailActivity.class);
                        try {
                            int id = jsonArray.getJSONObject(position).getInt("Id");
                            intent.putExtra("type",id);
                            getActivity().startActivity(intent);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }

        }

        @Override
        public int getItemCount() {
            if (null==jsonArray)return 0;
            return jsonArray.length();
        }
    }
    class MyWlsdViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView1,imageView2,imageView3,imageView4,imageView5;
        TextView textView1,textView2,textView3,textView4,textView5;
        LinearLayout ll,ll1;
        public MyWlsdViewHolder(View itemView) {
            super(itemView);
            imageView1= (ImageView) itemView.findViewById(R.id.wlsd_iv_1);
            imageView2= (ImageView) itemView.findViewById(R.id.wlsd_iv_2);
            textView1= (TextView) itemView.findViewById(R.id.wlsd_tv_1);
            textView2= (TextView) itemView.findViewById(R.id.wlsd_tv_2);
            textView3= (TextView) itemView.findViewById(R.id.wlsd_tv_3);
            textView4= (TextView) itemView.findViewById(R.id.wlsd_tv_4);
            textView5= (TextView) itemView.findViewById(R.id.wlsd_tv_5);
            imageView3= (ImageView) itemView.findViewById(R.id.ll_iv_1);
            imageView4= (ImageView) itemView.findViewById(R.id.ll_iv_2);
            imageView5= (ImageView) itemView.findViewById(R.id.ll_iv_3);
            ll= (LinearLayout) itemView.findViewById(R.id.wlsd_ll);
            ll1= (LinearLayout) itemView.findViewById(R.id.wlsd_ll_1);
        }
    }
    //解析json
    public void parseWlsd(){
        RequestParams params=new RequestParams("http://api.shiandianping.com/api/sourceAndroidDianPing/v3.1.0/Posts?boardId=3&pageSize=5&pageIndex=1");
        x.http().get(params, new Callback.CommonCallback<MyJsonObject>() {
            @Override
            public void onSuccess(MyJsonObject result) {
                try {
                    JSONArray data = result.getJSONArray("Data");
                    Log.i(TAG, "onSuccess: ");
                    myWlsdAdapter.updateItem(data);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}
